<%@ page import="vo.ScheduleVO"%>
<%@ page import="javax.swing.text.Document"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="dto.ScheduleDTO"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href ='${pageContext.request.contextPath }/css/calendar.css'>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:useBean id="vo" class="vo.ScheduleVO"/>
<jsp:setProperty property="*" name="vo"/>
<%
	ScheduleDTO dto = new ScheduleDTO();
	dto.setVo(vo);
	ArrayList<ScheduleVO> list = dto.getPrivateScheduleContents((String)session.getAttribute("sessionMid"));

	if(session.getAttribute("sessionMid") != null){
		String m_id = (String)session.getAttribute("sessionMid");
	}else{
		%><script type="text/javascript">alert(m_id);</script> <%
		%><script type="text/javascript">alert("로그인 페이지로 이동 합니다.");</script> <%
		%><script type="text/javascript">location.href = "/group2/index/index.jsp";</script> <%
	}
%>
<script type="text/javascript">
	var PopLayer = {
		nowLayer : "",
		openLayer : "",
		Xpos : "",
		Ypos : "",
		Action : function(strAnchor, strLayer){
			this.nowLayer = strLayer;
			if(this.openLayer != ""){
				var objOpenLayer = document.getElementById(this.openLayer);
				objOpenLayer.style.display = "none";
				this.openLayer = "";
			}
			this.openLayer = strLayer;
			try{
				strAnchor.onmouseup = PopLayer.Open;
			} catch(e) {}
		},
		Open : function(e){
			if(document.all){
				this.Xpos = event.clientX-200;
				this.Ypos = event.clientY-200;
				alert("Xpos: "+Xpos);
				alert("Ypos: "+Ypos);
				if(this.Xpos > 680){
					this.Xpos = 640;
				}
				if(this.Ypos > 400){
					this.Ypos = 350;
				}
			} else {
				this.Xpos = e.clientX-200;
				this.Ypos = e.clientY-200;
				if(this.Xpos > 680){
					this.Xpos = 640;
				}
				if(this.Ypos > 400){
					this.Ypos = 350;
				}
			}
			var objNowLayer = document.getElementById(PopLayer.nowLayer);
			
			objNowLayer.style.left = document.body.scrollLeft + this.Xpos + "px";
			objNowLayer.style.top = document.body.scrollTop + this.Ypos + "px";
			objNowLayer.style.display = "block";
		},
		Close : function(){
			this.openLayer = "";
			var objNowLayer = document.getElementById(PopLayer.nowLayer);
			objNowLayer.style.display = "none";
		}
	}
</script>
<title>calendar.jsp</title>
</head>
<body bgcolor="white">
<%
	int action = 0;
	int currYear = 0;
	int currMonth = 0;
	String boxSize = "70";
	
	Calendar c = Calendar.getInstance();
	Calendar cal = Calendar.getInstance();

	if (request.getParameter("action") == null){
		currMonth = c.get(c.MONTH);
		currYear = c.get(c.YEAR);
		cal.set(currYear, currMonth,1);
	}
	else{
		if(!(request.getParameter("action") == null)){
			currMonth = Integer.parseInt(request.getParameter("month"));
			currYear = Integer.parseInt(request.getParameter("year"));

			if(Integer.parseInt(request.getParameter("action")) == 1 ){
				cal.set(currYear, currMonth, 1);
				cal.add(cal.MONTH, 1);
				currMonth = cal.get(cal.MONTH);
				currYear = cal.get(cal.YEAR);
			}
			else{
				cal.set(currYear, currMonth , 1);
				cal.add(cal.MONTH, -1);
				currMonth = cal.get(cal.MONTH);
				currYear = cal.get(cal.YEAR);
			}
		}
	} 
%>
<%!public boolean isDate(int year, int month, int day){
		
		month -= 1;
        Calendar c = Calendar.getInstance();
        c.setLenient(false);

        try{
	        c.set(year, month, day);
	        Date dt = c.getTime();
        }catch (IllegalArgumentException e){
        	return false;
        }
       	return true;
    }%>
<%!public String getDateName (int monthNumber) {
		
		String strReturn = "";
		switch (monthNumber){ 
			case 0: strReturn = "1월"; break;
			case 1: strReturn = "2월"; break;
			case 2: strReturn = "3월"; break;
			case 3: strReturn = "4월"; break;
			case 4: strReturn = "5월"; break;
			case 5: strReturn = "6월"; break;
			case 6: strReturn = "7월"; break;
			case 7: strReturn = "8월"; break;
			case 8: strReturn = "9월"; break;
			case 9: strReturn = "10월"; break;
			case 10: strReturn = "11월"; break;
			case 11: strReturn = "12월"; break;
		}
		return strReturn;
    }%>
<p>   
<img alt="" src="../images/m_title_schedule_month.png">
<p>
	<table border="0" width="1000" height="13" style="margin-bottom: 10px; font-weight: bold;" align="center">
		<tr>
			<td width="100" align="center" valign="middle"><a href="${pageContext.request.contextPath }/index/main.jsp?inc=../schedule/schedule_private.jsp?month=<%=currMonth%>&year=<%=currYear%>&action=0&left_menu=../schedule/schedule_menu.jsp" style="text-decoration: none;"><font class="arrow" size="3" face="맑은 고딕" color="black">◀</font></a></td>		
			<td width="200" align="center" valign="middle"><div style="font-family: 맑은 고딕; font-size: 20px;"><%=cal.get(cal.YEAR)+"년&nbsp&nbsp"+getDateName(cal.get(cal.MONTH))%></div></td>
 			<td width="100" align="center" valign="middle"><a href="${pageContext.request.contextPath }/index/main.jsp?inc=../schedule/schedule_private.jsp?month=<%=currMonth%>&year=<%=currYear%>&action=1&left_menu=../schedule/schedule_menu.jsp" style="text-decoration: none;"><font class="arrow" size="3" face="맑은 고딕" color="black">▶</font></a></td>
		</tr>
	</table>
	<table border="1" width="1000" style="border-collapse: collapse" bordercolor="#000000" bgcolor="#DFDCD8" align="center">
	  	<tr>
	   		<td width="<%=boxSize%>" height="25" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>일 요 일</b></font>
	   		</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>월 요 일</b></font>
	   		</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>화 요 일</b></font>
	   		</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
				<font color="#FFFFFF" size="2" face="맑은 고딕"><b>수 요 일</b></font>
			</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>목 요 일</b></font>
	   		</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>금 요 일</b></font>
	   		</td>
	   		<td width="<%=boxSize%>" height="20" align="center" nowrap bordercolor="#000000" bgcolor="#666666">
	   			<font color="#FFFFFF" size="2" face="맑은 고딕"><b>토 요 일</b></font>
	   		</td>
	  	</tr>
		<%
			final String NULL_DATA = null;
			
			int currDay;
			String todayColor;
			int count = 1;
			int dispDay = 1;
			String date_popup = "";
			ScheduleVO v = null;
		%>
		<%for(int i=1; i<7; i++){ %>
			<tr><%for(int j=1; j<8; j++){ %>
					<%if(!(count >= cal.get(c.DAY_OF_WEEK))){ %>
						<td width="<%=boxSize%>" height="<%=boxSize%>" valign="top" align="left" bgcolor="#DFDCD8">&nbsp;</td>
						<%count += 1; %>
					<%}else{ %>
						<%if(isDate(currYear, currMonth+1, dispDay)){ %>
							<%if (dispDay == c.get(c.DAY_OF_MONTH) && c.get(c.MONTH) == cal.get(cal.MONTH) && c.get(c.YEAR) == cal.get(cal.YEAR)){ %>
								<%todayColor = "#6C7EAA"; %>
							<%}else { %>
								<%todayColor = "#ffffff"; %>
							<%} %>
							<%if((dispDay < 10) && ((currMonth+1) < 10)){ %>
								<%date_popup = String.valueOf(currYear)+String.valueOf("0"+(currMonth+1))+String.valueOf("0"+dispDay); %>
							<%}else if((dispDay < 10) && ((currMonth+1) > 9)){ %>
								<%date_popup = String.valueOf(currYear)+String.valueOf((currMonth+1))+String.valueOf("0"+dispDay); %>
							<%}else if((currMonth+1) < 10 && (dispDay > 9)){ %>
								<%date_popup = String.valueOf(currYear)+String.valueOf("0"+(currMonth+1))+String.valueOf(dispDay); %>
							<%}else{ %>						
								<%date_popup = String.valueOf(currYear)+String.valueOf(currMonth+1)+String.valueOf(dispDay); %>
							<%} %>
							<td bgcolor ="<%=todayColor%>" width="<%=boxSize%>" align="left" height="<%=boxSize%>" valign="top" onmousedown="PopLayer.Action(this,'layer<%=date_popup %>')"><div style="font-size: 13px; font-family: 맑은 고딕; padding-top: 5px; padding-left: 5px; table-layout: fixed;"><%=dispDay%></div>
								<%for(int l=0; l<list.size(); l++){ %>
									<%v = list.get(l); %>
									<%if(v.getSchedule_date().equals(date_popup)){ %>
										<%if(v.getSchedule_contents_first() != null){ %>
											<div style="background-color: #B2CCFF; font-family: 맑은 고딕; font-size: 13px; padding-left: 5px;" align="center"><%=v.getSchedule_contents_first() %></div>
										<%} %>
										<%if(v.getSchedule_contents_second() != null){ %>
											<div style="background-color: #FFD8D8; font-family: 맑은 고딕; font-size: 13px; padding-left: 5px;" align="center"><%=v.getSchedule_contents_second() %></div>
										<%} %>
									<%}%>
								<%} %>
							</td>
							<%for(int p=1; p<=dispDay; p++){ %>
								<%String year = date_popup.substring(0, 4)+"년 ";%>
								<%String month = date_popup.substring(4, 6)+"월 ";%>
								<%String day = date_popup.substring(6, 8)+"일";%>
								<%String ymd = year+month+day; %>
								<%for(int s=0; s<list.size(); s++){ %>
									<%ScheduleVO v1 = list.get(s); %>
									<%if(v1.getSchedule_date().equals(date_popup)){ %>
										<div id="layer<%=date_popup %>" style="border: solid 1px #8C8C8C; width: 400px; height: 200px; display:none; position:absolute; left:0; top:0; padding: 10px; background-color: #F6F6F6; font-size:12px; font-family: 맑은 고딕; color: black;">		 
											<font size="4" style="text-align: left;"><%=ymd %></font>
											<form name="frm_ud" method="post" action="">
												<%if(v1.getSchedule_contents_first() == null && v1.getSchedule_contents_second() != null){ %>
													<input id="m_id" name="m_id" type="hidden" value="<%=session.getAttribute("sessionMid") %>"/>
													<input id="schedule_title" name="schedule_title" type="hidden" value="난 수지에용~"/>
													<input id="schedule_date" name="schedule_date" type="hidden" value="<%=date_popup %>"/>
													<input id="schedule_division" name="schedule_division" type="hidden" value="private"/>
													<input id="schedule_all_delete" name="schedule_all_delete" type="hidden" value="allDelete"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">첫번째 일정</div>
													<input id="schedule_contents_first" name="schedule_contents_first" type="text" placeholder="일정을 입력하세요." size="43">
													<input id="btnInsert" name="btnInsert" type="submit" value="일정 등록" formaction="/group2/schedule_private_first_insert.do"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">두번째 일정</div>
													<input id="schedule_contents_second" name="schedule_contents_second" type="text" placeholder="일정을 입력하세요." value="<%=v1.getSchedule_contents_second() %>" size="43">
													<input id="btnUpdate" name="btnUpdate" type="submit" value="수정" formaction="/group2/schedule_private_second_update.do"/>
													<input id="btnDelete" name="btnDelete" type="submit" value="삭제" formaction="/group2/schedule_private_second_delete.do"/>
													<br/><br/><input type="button" value="닫기" onclick="PopLayer.Close()"/>
												<%} %>
												<%if(v1.getSchedule_contents_first() != null && v1.getSchedule_contents_second() == null){ %>
													<input id="m_id" name="m_id" type="hidden" value="<%=session.getAttribute("sessionMid") %>"/>
													<input id="schedule_title" name="schedule_title" type="hidden" value="난 수지에용~"/>
													<input id="schedule_date" name="schedule_date" type="hidden" value="<%=date_popup %>"/>
													<input id="schedule_division" name="schedule_division" type="hidden" value="private"/>
													<input id="schedule_all_delete" name="schedule_all_delete" type="hidden" value="allDelete"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">첫번째 일정</div>
													<input id="schedule_contents_first" name="schedule_contents_first" type="text" placeholder="일정을 입력하세요." value="<%=v1.getSchedule_contents_first() %>" size="43">
													<input id="btnUpdate" name="btnUpdate" type="submit" value="수정" formaction="/group2/schedule_private_first_update.do"/>
													<input id="btnDelete" name="btnDelete" type="submit" value="삭제" formaction="/group2/schedule_private_first_delete.do"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">두번째 일정</div>
													<input id="schedule_contents_second" name="schedule_contents_second" type="text" placeholder="일정을 입력하세요." size="43">
													<input id="btnInsert" name="btnInsert" type="submit" value="일정 등록" formaction="/group2/schedule_private_second_insert.do"/>
													<br/><br/><input type="button" value="닫기" onclick="PopLayer.Close()"/>					
												<%} %>
												<%if(v1.getSchedule_contents_first() != null && v1.getSchedule_contents_second() != null){ %>
													<input id="m_id" name="m_id" type="hidden" value="<%=session.getAttribute("sessionMid") %>"/>
													<input id="schedule_title" name="schedule_title" type="hidden" value="난 수지에용~"/>
													<input id="schedule_date" name="schedule_date" type="hidden" value="<%=date_popup %>"/>
													<input id="schedule_division" name="schedule_division" type="hidden" value="private"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">첫번째 일정</div>
													<input id="schedule_contents_first" name="schedule_contents_first" type="text" placeholder="일정을 입력하세요." value="<%=v1.getSchedule_contents_first() %>" size="43">
													<input id="btnUpdate" name="btnUpdate" type="submit" value="수정" formaction="/group2/schedule_private_first_update.do"/>
													<input id="btnDelete" name="btnDelete" type="submit" value="삭제" formaction="/group2/schedule_private_first_delete.do"/>
													<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">두번째 일정</div>
													<input id="schedule_contents_second" name="schedule_contents_second" type="text" placeholder="일정을 입력하세요." value="<%=v1.getSchedule_contents_second() %>" size="43">
													<input id="btnUpdate" name="btnUpdate" type="submit" value="수정" formaction="/group2/schedule_private_second_update.do"/>
													<input id="btnDelete" name="btnDelete" type="submit" value="삭제" formaction="/group2/schedule_private_second_delete.do"/>
													<br/><br/><input type="button" value="닫기" onclick="PopLayer.Close()"/>					
												<%} %>
											</form>
										</div>
									<%} %>
								<%} %> 
								<div id="layer<%=date_popup %>" style="border: solid 1px #8C8C8C; width: 400px; height: 200px; display:none; position:absolute; left:0; top:0; padding: 10px; background-color: #F6F6F6; font-size:12px; font-family: 맑은 고딕; color: black;">
								<font size="4" style="text-align: left;"><%=ymd %></font>
									<form name="frm_ud" method="post">
										<br/><br/><div style="font-family: 맑은 고딕; font-size: 15px">첫번째 일정</div>
										<input id="m_id" name="m_id" type="hidden" value="<%=session.getAttribute("sessionMid") %>"/>
										<input id="schedule_title" name="schedule_title" type="hidden" value="난 수지에용~"/>
										<input id="schedule_date" name="schedule_date" type="hidden" value="<%=date_popup %>"/>
										<input id="schedule_division" name="schedule_division" type="hidden" value="private"/>
										<input id="schedule_contents_first" name="schedule_contents_first" type="text" placeholder="일정을 입력하세요." size="43">
										<input id="schedule_contents_second" name="schedule_contents_second" type="hidden" placeholder="일정을 입력하세요." value="<%=NULL_DATA %>">
										<input id="btnInsert" name="btnInsert" type="submit" value="일정 등록" formaction="/group2/schedule_private_first_insert.do"/>
										<br/><br/><input type="button" value="닫기" onclick="PopLayer.Close()"/>
									</form>
								</div>
								<%} %>
							<%count += 1; dispDay += 1; %>
						<%}else{ %>
							<td width="<%=boxSize%>" align="left" height="<%=boxSize%>" valign="top" bgcolor="#DFDCD8" style="table-layout: fixed;">&nbsp;</td>
						<%} %> 
					<%} %>
				<%} %>
	  		</tr> 
		<%} %>
	</table>
</body>
</html>